package com.example.team01_task21;

import java.util.concurrent.TimeUnit;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.team01_task21.MyService.LocalBinder;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
	MyService myService;
	private boolean isBound;
	Button btn, btnClose;
	private SeekBar seekbar;
	TextView tvCurrentTime, tvTotalTime;
	private Handler myHandler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.btnPause);
		seekbar = (SeekBar) findViewById(R.id.seekBarTime);
		tvCurrentTime = (TextView) findViewById(R.id.txtCurentTime);
		tvTotalTime = (TextView) findViewById(R.id.txtTimeSong);

		isBound = false;

		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				playAudio();
			}
		});

		btnClose = (Button) findViewById(R.id.btnClose);
		btnClose.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				finish();
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void playAudio() {
		Intent objIntent = new Intent(this, MyService.class);
		if (!isBound) {
			bindService(objIntent, myConnection, Context.BIND_AUTO_CREATE);
			isBound = true;
			btn.setBackgroundResource(R.drawable.pause);// setText("Pause");
			startService(objIntent);

		} else {
			myService.plauseAudio();
			btn.setBackgroundResource(R.drawable.play);
			isBound = false;
			unbindService(myConnection);
		}

	}

	private Runnable UpdateSongTime = new Runnable() {
		@SuppressLint("NewApi")
		@TargetApi(Build.VERSION_CODES.GINGERBREAD)
		public void run() {

			if (isBound) {
				tvCurrentTime.setText(String.format(
						"%d min, %d sec",
						TimeUnit.MILLISECONDS.toMinutes((long) myService
								.getStartTime()),
						TimeUnit.MILLISECONDS.toSeconds((long) myService
								.getStartTime())
								- TimeUnit.MINUTES
										.toSeconds(TimeUnit.MILLISECONDS
												.toMinutes((long) myService
														.getStartTime()))));

				seekbar.setProgress((int) myService.getStartTime());
				myHandler.postDelayed(this, 100);
			}
		}
	};

	public void stopAudio() {

		Intent objIntent = new Intent(this, MyService.class);
		if (isBound) {
			isBound = false;
			unbindService(myConnection);
			stopService(objIntent);

		} else {
			stopService(objIntent);
		}
		btn.setBackgroundResource(R.drawable.play);
	}

	private ServiceConnection myConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName className, IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			myService = binder.getService();

			tvCurrentTime
					.setText(String.format(
							"%d min, %d sec",
							TimeUnit.MILLISECONDS.toMinutes((long) myService
									.getStartTime()),
							TimeUnit.MILLISECONDS.toSeconds((long) myService
									.getStartTime())
									- TimeUnit.MINUTES
											.toSeconds(TimeUnit.MILLISECONDS
													.toMinutes((long) myService
															.getStartTime()))));

			tvTotalTime
					.setText(String.format(
							"%d min, %d sec",
							TimeUnit.MILLISECONDS.toMinutes((long) myService
									.getFinalTime()),
							TimeUnit.MILLISECONDS.toSeconds((long) myService
									.getFinalTime())
									- TimeUnit.MINUTES
											.toSeconds(TimeUnit.MILLISECONDS
													.toMinutes((long) myService
															.getFinalTime()))));

			seekbar.setMax((int) myService.getFinalTime());
			seekbar.setProgress((int) myService.getStartTime());
			myHandler.postDelayed(UpdateSongTime, 100);

			isBound = true;
		}

		public void onServiceDisconnected(ComponentName arg0) {
			isBound = false;
		}

	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (isBound) {
			// Disconnect from an application service. You will no longer
			// receive calls as the service is restarted, and the service is
			// now allowed to stop at any time.
			stopAudio();
		}
	}

}
